<template>
    <div>
        <h1>{{id?'修改':'新建'}}英雄列表</h1>
        <el-form label-width="123px" @submit.native.prevent = 'save'>
        <el-tabs type="border-card" value="basic">
            <el-tab-pane label="基本信息" name="basic">
                        <el-form-item label="名称">
                <el-input v-model="model.name"></el-input>
            </el-form-item>
              <el-form-item label="称号">
                <el-input v-model="model.title"></el-input>
            </el-form-item>
            <el-form-item label="冷却值">
                <el-input v-model="model.delay"></el-input>
            </el-form-item>
            <el-form-item label="消耗值">
                <el-input v-model="model.cost"></el-input>
            </el-form-item>
             <el-form-item label="类型">
                <el-select v-model="model.categories" multiple > 
                    <el-option v-for="item in parents" :key="item._id"
                    :label="item.name" :value="item._id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="头像">
                <el-upload
                 class="avatar-uploader"
                    :action="getactionURL"             
                    :headers="getAuthrization()"
                    :show-file-list="false"
                    :on-success="res=>$set(model,'avater',res.url)">
                    <img v-if="model.avater" :src="model.avater" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                    </el-form-item>
                      <el-form-item label="banner">
                <el-upload
                 class="avatar-uploader"
                    :action="getactionURL"             
                    :headers="getAuthrization()"
                    :show-file-list="false"
                    :on-success="res=>$set(model,'banner',res.url)">
                    <img v-if="model.banner" :src="model.banner" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                    </el-form-item>
             <el-form-item label="难度">
                <el-rate style="margin-top:0.7rem;" :max="9" v-model="model.scores.difficult"></el-rate>
            </el-form-item>
            <el-form-item label="技能">
                <el-rate style="margin-top:0.7rem;" :max="9" v-model="model.scores.skills"></el-rate>
            </el-form-item>
            <el-form-item label="攻击">
                <el-rate style="margin-top:0.7rem;" :max="9" v-model="model.scores.attack"></el-rate>
            </el-form-item>
            <el-form-item label="生存">
                <el-rate style="margin-top:0.7rem;" :max="9" v-model="model.scores.survive"></el-rate>
            </el-form-item>
             <el-form-item label="顺风出装">
                <el-select v-model="model.item1" multiple > 
                    <el-option v-for="item in items" :key="item._id"
                    :label="item.name" :value="item._id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="逆风出装">
                <el-select v-model="model.item2" multiple > 
                    <el-option v-for="item in items" :key="item._id"
                    :label="item.name" :value="item._id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="使用技巧">
                <el-input v-model="model.usageTips"></el-input>
            </el-form-item>
            <el-form-item label="对抗技巧">
                <el-input v-model="model.battleTips"></el-input>
            </el-form-item>
            <el-form-item label="团战思路">
                <el-input v-model="model.teamTips"></el-input>
            </el-form-item>
            
            </el-tab-pane>
            <el-tab-pane label="技能" name="skills"> 
                <el-button size="small" @click="model.skills.push({})"><i class="el-icon-plus"></i>添加技能</el-button>
                <el-row type="flex" style="flex-wrap: wrap">
                    <el-col :md="12" v-for="(item,index) in model.skills" :key="index">
                        <el-form-item label="名称">
                            <el-input v-model="item.name"></el-input>
                        </el-form-item>
                       <el-form-item label="图标">
                <el-upload
                 class="avatar-uploader"
                    :action="getactionURL"             
                    :headers="getAuthrization()"
                    :show-file-list="false"
                    :on-success="res=>{$set(item,'icon',res.url)}">
                    <img v-if="item.icon" :src="item.icon" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                    </el-form-item>
                        <el-form-item label="描述">
                            <el-input v-model="item.description"></el-input>
                        </el-form-item>
                        <el-form-item label="小提示">
                            <el-input v-model="item.tips"></el-input>
                        </el-form-item>
                        <el-form-item >
                            <el-button size="small" type="danger" @click="model.skills.splice(index,1)">删除</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
                </el-tab-pane>

                <el-tab-pane label="最佳搭档" name="partners"> 
                <el-button size="small" @click="model.partners.push({})"><i class="el-icon-plus"></i>添加英雄</el-button>
                <el-row type="flex" style="flex-wrap: wrap">
                    <el-col :md="12" v-for="(item,index) in model.partners" :key="index">
                        <el-form-item label="英雄">
                            <el-select filterable  v-model="item.hero"> 
                                 <el-option v-for="heros in heroes" :key="heros._id"
                            :label="heros.name" :value="heros._id"></el-option>
                                    </el-select>
                        </el-form-item>
                        <el-form-item label="描述">
                            <el-input v-model="item.description" type="textarea"></el-input>
                        </el-form-item>
                        <el-form-item >
                            <el-button size="small" type="danger" @click="model.partners.splice(index,1)">删除</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
                </el-tab-pane>
        </el-tabs>
                
            <el-button type="primary" native-type="submit">保存</el-button>
        </el-form>
    </div>
</template>

<script>
export default {
    data() {
        return {
            model:{
                 avater:'' ,
                 scores:{
                     difficult:0
                 },
                 skills:[],
                 partners:[]
            },   
            heroes:[],
            items:[],
            parents:[]  
        }
    },
    props:{
        id:{}
    },
    methods: {
        async save(){
            let res
            if(this.id){
                res = await this.$http.put(`rest/Hero/${this.id}`,this.model)
            }else{
                res = await this.$http.post('rest/Hero',this.model)         
            }   
               this.$router.push('/Hero/list')                 
                this.$message({
                type:'success',
                message:"保存成功了"
            })      
        },
        async fetch(){
            const res  = await this.$http.get(`rest/Hero/${this.id}`)
            this.model = {...this.model,...res.data}
            
        },
        async parentsfetch(){
            const res  = await this.$http.get(`rest/categories`)
            this.parents = res.data
        },
         async Itemfetch(){
            const res  = await this.$http.get(`rest/Item`)
            this.items = res.data
        },
         async heroesfetch(){
            const res  = await this.$http.get(`rest/Hero`)
            this.heroes = res.data
        }
    },
    created() {
        this.parentsfetch()
        this.heroesfetch()
         this.Itemfetch()
        this.id && this.fetch()
    },
};
</script>

<style scoped>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 5rem;
    height: 5rem;
    line-height: 5rem;
    text-align: center;
  }
  .avatar {
    width: 5rem;
    height: 5rem;
    display: block;
  }
</style>